#include "PlayerHealthBar.h"

#define PROCESSBAR_TAG 1


PlayerHealthBar::PlayerHealthBar(void)
{
	_percent = 100;
	_processBar = NULL;
}


PlayerHealthBar::~PlayerHealthBar(void)
{
	CC_SAFE_RELEASE(_processBar);
}

bool PlayerHealthBar::init()
{
	if (!CCNode::init())
		return false;

	int width = 160;

	CCSprite* border = CCSprite::createWithSpriteFrameName("healtbar_border.png");
	border->setAnchorPoint(CCPointZero);
	border->setPosition(ccp(40, 5));
	border->setScaleX(1);
	addChild(border, 0);



	CCSprite* process = CCSprite::createWithSpriteFrameName("healtbar_process.png");
	process->setAnchorPoint(CCPointZero);
	process->setPosition(CCPointZero);
	process->setFlipY(true);

	CCRenderTexture* textRen = CCRenderTexture::create(process->getContentSize().width, process->getContentSize().height);
	//textRen->beginWithClear(255, 255, 255, 255);
	textRen->begin();
	process->visit();
	textRen->end();

	//process->setPosition(ccp(47, 7));
	//addChild(textRen);

	setProcessBar(textRen->getSprite()->getTexture());

	CCSprite* icon = CCSprite::createWithSpriteFrameName("healtbar_icon.png");
	icon->setAnchorPoint(CCPointZero);
	addChild(icon, 1);
	setPercent(100);
	return true;
}

float PlayerHealthBar::getPercent()
{
	return _percent;
}

void PlayerHealthBar::setPercent(float var)
{
	if (var < 0)
		var = 0;
	if (var > 100)
		var = 100;
	_percent = var;

	// 188 26
	removeChildByTag(PROCESSBAR_TAG);
	
	CCRect rect = CCRectMake(188 - 188 * _percent / 100, 0, 188 * _percent / 100, 26);
	
	CCSprite* bar = CCSprite::createWithTexture(_processBar, rect);
	
	bar->setAnchorPoint(CCPointZero);
	bar->setPosition(ccp(40, 5));
	
	addChild(bar, 0, PROCESSBAR_TAG);
}
